<!DOCTYPE html>
<html lang="en">
    <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            
            
            
            <link rel="shortcut icon" href="../../../img/favicon.ico">
        
        <script type="text/javascript" src="//game.bmob.cn/static/doc_union.js"></script>
            <title>Bmob文档中心</title>
            <link href="../../../css/bootstrap-custom.min.css" rel="stylesheet">
            <link href="../../../css/font-awesome-4.5.0.css" rel="stylesheet">
            <link href="../../../css/base.css" rel="stylesheet">
            <link rel="stylesheet" href="../../../css/highlight.css">
            <link href="../../../css/agate.css" rel="stylesheet">
            <link href="../../../css/custom.css" rel="stylesheet" id="custom">
    </head>

    <body >

        <div class="navbar navbar-fixed-top" role="navigation">
    <div class="main-nav">
        <!-- Collapsed navigation -->
        <div class="navbar-header">
            <a class="navbar-brand" href="../../..">
                <!--Bmob文档中心-->
                <img src="../../../img/logo.png" alt="">
            </a>
        </div>

        <!-- Expanded navigation -->
        <div class="navbar-collapse collapse">

          <ul class="nav navbar-nav">
            
              <li class="pull-left">
                
					<a href="../../..">文档首页</a>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">数据服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">数据服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../../data/android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../../../data/ios/">iOS</a>
                                </li>
                            
                            
								<li >
									<a href="../../../data/csharp/">C#</a>
                                </li>
                            
                            
								<li >
									<a href="../../../data/php/">PHP</a>
                                </li>
                            
                            
								<li >
									<a href="../../../data/go/">GO</a>
                                </li>
                            
                            
								<li >
									<a href="../../../data/restful/">RESTful</a>
                                </li>
                            
                            
								<li >
									<a href="../../../data/javascript/">JavaScript</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../../data/cocos2d_x/">Cocos2D-X</a>
                                </li>
							
                            
								<li >
									<a href="../../../data/wechat_app/">小程序</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../../data/wechat_app_new/">快应用</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../data/wechat_app_new/">Nodejs</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../data/wechat_app_new/">Cocos Creator</a>
                                </li>
							
                            
								<li >
									<a href="../../../data/wechat_app_new/rm/">小程序(新)</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../../cloud_function/android/">云函数</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">云函数 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../../cloud_function/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/java/">Java</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/csharp/">C#</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/restful/">RESTful</a>
                                </li>
							
                            
								<li >
									<a href="../../../cloud_function/web/">Web</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../../sms/android/">短信服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">短信服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../../sms/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../sms/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../sms/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../sms/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../sms/restful/">RESTful</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left active">
                
					<!--<a href="">游戏实时后端</a>-->
					<li class="dropdown active">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">游戏实时后端 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../unity/quick_start/">Unity</a>
                                </li>
                            
                            
								<li >
									<a href="../../cocos_creator/quick_start/">Cocos Creator</a>
                                </li>
                            
                            
								<li >
									<a href="../../wechat_games/quick_start/">微信小游戏</a>
                                </li>
                            
                            
                                <li class="active">
                                    <a href="./">云函数</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../classic_case/">经典案例</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">IM服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">IM服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../../im/android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../../../im/ios/">iOS</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../../push/android/">推送服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">推送服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../../push/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../push/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../push/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../push/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../push/restful/">RESTful</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../../other/common_problem/">其他</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">其他 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../../other/common_problem/">常见问题</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../other/error_code/">错误码</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../other/data_safety/">数据安全</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../other/bql/">BQL</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li>
                  <a href="https://www.bmob.cn/repository/index" target="_blank">知识库</a>
              </li>
              <li>
                  <a href="http://doc.bmob.cn/video/index.html" target="_blank">视频教程</a>
              </li>
            </ul>
            
        </div>
    </div>
</div>

        <div class="pagebody" id="main-wrapper">
            <div class="sidebar">
                <div class="bs-sidebar hidden-print affix well" role="complementary">

	
		
	
		
				
				
				
				
				
				
				
				
				
				
				
				
				
		
	
		
				
				
				
				
				
				
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
				
				
					<div class="code-title">云函数</div>
				
				
		
	
		
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
				
				
		
	


	<ul class="nav bs-sidenav">
		
			
		
			
		
			
		
			
		
			
				
					
						
					
						
					
						
					
						
							
								
									
										<li class="active "><a href="#_1">开发环境</a>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#ide">IDE</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#jar">Jar文件</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_2">步骤</a></li>
											</ul>
											
										</li>
									
										<li class=""><a href="#roomjava">Room.java</a>
											
										</li>
									
										<li class=""><a href="#playerjava">Player.java</a>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_3">获取属性</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_4">修改属性</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_5">监听属性变化</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_6">处理客户端行为</a></li>
											</ul>
											
										</li>
									
										<li class=""><a href="#bmob">Bmob</a>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_7">初始化</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_8">操作数据库</a></li>
											</ul>
											
										</li>
									
								
							
						
					
						
					
				
			
		
			
		
			
		
			
		
	</ul>
</div>
            </div>
            <div class="content" role="main">
                <div class="wrap">
                    

<ul>
<li>BGS的云函数可以完美实现游戏的后端逻辑层，并且有热更新机制，可以随时修改、升级</li>
<li>缝合了Bmob数据服务，可以快速进行Bmob数据库的增删查改，其中 <code>Bmob.class</code> 的用法与 <a href="http://doc.bmob.cn/cloud_function/java/">Bmob Java云函数</a> 的 <code>modules.oData</code> 完全一致</li>
</ul>
<p>主要需要开发者实现的有 <code>Room.java</code>、<code>Player.java</code></p>
<hr />
<h2 id="_1">开发环境<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h2>
<h3 id="ide">IDE<a class="headerlink" href="#ide" title="Permanent link">&para;</a></h3>
<p>建议使用<code>Eclipse</code>，也可以用AS、Sublime等支持Java语言的IDE</p>
<h3 id="jar">Jar文件<a class="headerlink" href="#jar" title="Permanent link">&para;</a></h3>
<p><a href="//game.bmob.cn/static/BmobGameSDKCloud.jar">下载地址</a></p>
<h3 id="_2">步骤<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3>
<ol>
<li>在Eclipse创建一个java项目</li>
<li>在该项目下，如果没有的话，创建src、libs文件夹</li>
<li>将从管理后台复制的Room、Player代码，或者demo带的cloud文件夹里的java文件，添加到src文件夹下，包名为<strong>cn.bmob.gamesdk.server.custom</strong></li>
<li>将下载的Jar文件，拖到libs文件夹下</li>
<li>右键点击项目 &gt; <code>Build Path</code> &gt; <code>Configure Build Path</code>，左侧选择<code>Java Build Path</code>，右侧选项卡选择 <code>Libraries</code>，点击 <code>Add JARs...</code> ，找到jar文件，保存</li>
<li>在Room.java、Player.java文件内编辑代码，此时就有代码提示了</li>
<li>将两个文件的代码都复制到管理后台后，点击保存(不是复制一个文件点击保存一次)</li>
</ol>
<hr />
<h2 id="roomjava">Room.java<a class="headerlink" href="#roomjava" title="Permanent link">&para;</a></h2>
<p>继承自 <code>RoomBase.class</code> , 作用是管理、监控房间的生命周期</p>
<p><strong>以下是类属性</strong> ：</p>
<table>
<thead>
<tr>
<th align="center">名称</th>
<th align="center">类型</th>
<th align="center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">roomId</td>
<td align="center">int</td>
<td align="center">房间id，创建房间时产生，客户端SDK加入房间时需要携带</td>
</tr>
<tr>
<td align="center">players</td>
<td align="center">Player[]</td>
<td align="center">该房间内所有玩家，索引为玩家的no，长度为playerCount</td>
</tr>
<tr>
<td align="center">playerCount</td>
<td align="center">int</td>
<td align="center">该房间内玩家数</td>
</tr>
<tr>
<td align="center">masterId</td>
<td align="center">String</td>
<td align="center">创建房间的玩家id</td>
</tr>
<tr>
<td align="center">masterKey</td>
<td align="center">String</td>
<td align="center">销毁/踢出玩家需要携带的key</td>
</tr>
<tr>
<td align="center">joinKey</td>
<td align="center">String</td>
<td align="center">加入房间需要携带的key</td>
</tr>
<tr>
<td align="center">isPlaying</td>
<td align="center">boolean</td>
<td align="center">该房间是在游戏中还是等待中</td>
</tr>
<tr>
<td align="center">startTime</td>
<td align="center">long</td>
<td align="center">该房间的游戏开始时间毫秒数</td>
</tr>
<tr>
<td align="center">tcpPort</td>
<td align="center">int</td>
<td align="center">该房间服务器的Tcp端口号</td>
</tr>
<tr>
<td align="center">udpPort</td>
<td align="center">int</td>
<td align="center">该房间服务器的Udp端口号</td>
</tr>
<tr>
<td align="center">websocketPort</td>
<td align="center">int</td>
<td align="center">该房间服务器的Web Socket端口号</td>
</tr>
<tr>
<td align="center">address</td>
<td align="center">String</td>
<td align="center">该房间所在的服务器地址</td>
</tr>
<tr>
<td align="center">bigEndian</td>
<td align="center">BigEndian</td>
<td align="center">用于传递int、float、double时编解码的工具对象</td>
</tr>
</tbody>
</table>
<p><strong>以下是可主动调用的方法</strong> ：</p>
<table>
<thead>
<tr>
<th align="center">方法名</th>
<th align="center">参数</th>
<th align="center">返回值</th>
<th align="center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">sendToAll</td>
<td align="center">byte[]</td>
<td align="center">boolean</td>
<td align="center">向所有玩家推送消息</td>
</tr>
<tr>
<td align="center">sendToAllExcept</td>
<td align="center">byte[],Player</td>
<td align="center">boolean</td>
<td align="center">向某玩家以外的所有玩家推送消息</td>
</tr>
<tr>
<td align="center">dispatchGameStart</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">让房间游戏开始</td>
</tr>
<tr>
<td align="center">dispatchGameOver</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">让房间游戏结束</td>
</tr>
<tr>
<td align="center">dispatchRoomDestroy</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">销毁房间</td>
</tr>
<tr>
<td align="center">isStrEmpty</td>
<td align="center">String</td>
<td align="center">boolean</td>
<td align="center">判断String对象是否为空</td>
</tr>
<tr>
<td align="center">getTime</td>
<td align="center">-</td>
<td align="center">long</td>
<td align="center">获取当前毫秒</td>
</tr>
<tr>
<td align="center">arraycopy</td>
<td align="center">参考System</td>
<td align="center">-</td>
<td align="center">复制数组</td>
</tr>
</tbody>
</table>
<p><strong>以下是需要Override的生命周期相关监听方法</strong> ：</p>
<p><em>这些方法都没有参数，返回值均为void</em></p>
<table>
<thead>
<tr>
<th align="center">方法名</th>
<th align="center">调用时机</th>
<th align="center">使用案例</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">onCreate</td>
<td align="center">房间被创建时</td>
<td align="center">将房间的 <code>id</code>、<code>joinKey</code>等保存到 <code>Bmob数据库</code>，可进行好友对战、匹配对战</td>
</tr>
<tr>
<td align="center">onGameStart</td>
<td align="center">所有玩家均已准备，游戏开始时</td>
<td align="center">初始化物品数量和位置、安全区的位置</td>
</tr>
<tr>
<td align="center">onTick</td>
<td align="center">游戏中，以每秒多次的频率调用(取决于每秒帧率配置)</td>
<td align="center">实现安全区、轰炸区等游戏设定，需要加上 <code>@BmobGameSDKHook</code> 注解</td>
</tr>
<tr>
<td align="center">onDestroy</td>
<td align="center">房间被销毁时</td>
<td align="center">将房间信息从 <code>Bmob数据库</code> 删除</td>
</tr>
</tbody>
</table>
<hr />
<h2 id="playerjava">Player.java<a class="headerlink" href="#playerjava" title="Permanent link">&para;</a></h2>
<p>继承自 <code>PlayerBase.class</code> , 作用有：
1. 管理、监控生命周期
2. 修改玩家属性值(editable==false的属性)
3. 监听玩家属性值变动(editable==true的属性)</p>
<p><strong>以下是类属性</strong> ：</p>
<table>
<thead>
<tr>
<th align="center">名称</th>
<th align="center">类型</th>
<th align="center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">room</td>
<td align="center">Room</td>
<td align="center">房间对象</td>
</tr>
<tr>
<td align="center">no</td>
<td align="center">int</td>
<td align="center">玩家在该房间的id，加入房间时分配</td>
</tr>
<tr>
<td align="center">roommates</td>
<td align="center">Player[]</td>
<td align="center">该房间内所有玩家，可以用roommates[no]进行索引</td>
</tr>
</tbody>
</table>
<p><strong>以下是可主动调用的方法</strong> ：</p>
<table>
<thead>
<tr>
<th align="center">方法名</th>
<th align="center">参数</th>
<th align="center">返回值</th>
<th align="center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">syncToClient</td>
<td align="center">-</td>
<td align="center">void</td>
<td align="center">修改参数结束后，将修改同步到客户端</td>
</tr>
<tr>
<td align="center">getStatus</td>
<td align="center">-</td>
<td align="center">int</td>
<td align="center">获取玩家状态，有无人/等待/准备/游戏中/被淘汰/掉线</td>
</tr>
<tr>
<td align="center">getUserId</td>
<td align="center">-</td>
<td align="center">String</td>
<td align="center">获取加入房间时传入的用户id</td>
</tr>
<tr>
<td align="center">send</td>
<td align="center">byte[]</td>
<td align="center">boolean</td>
<td align="center">向本玩家推送消息</td>
</tr>
<tr>
<td align="center">sendToAll</td>
<td align="center">byte[]</td>
<td align="center">boolean</td>
<td align="center">向该房间的所有玩家推送消息</td>
</tr>
<tr>
<td align="center">sendToOthers</td>
<td align="center">byte[]</td>
<td align="center">boolean</td>
<td align="center">向本玩家以外的所有玩家推送消息</td>
</tr>
<tr>
<td align="center">kick</td>
<td align="center">-</td>
<td align="center">boolean</td>
<td align="center">将本玩家踢出房间</td>
</tr>
<tr>
<td align="center">isStrEmpty</td>
<td align="center">String</td>
<td align="center">boolean</td>
<td align="center">判断String对象是否为空</td>
</tr>
<tr>
<td align="center">getTime</td>
<td align="center">-</td>
<td align="center">long</td>
<td align="center">获取当前毫秒</td>
</tr>
<tr>
<td align="center">arraycopy</td>
<td align="center">参考System</td>
<td align="center">-</td>
<td align="center">复制数组</td>
</tr>
</tbody>
</table>
<p><strong>以下是需要Override的生命周期相关监听方法</strong> ：</p>
<p><em>这些方法都没有参数，返回值均为void</em></p>
<table>
<thead>
<tr>
<th align="center">方法名</th>
<th align="center">调用时机</th>
<th align="center">使用案例</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">onJoin</td>
<td align="center">玩家加入房间时</td>
<td align="center">操作Bmob数据库</td>
</tr>
<tr>
<td align="center">onLeave</td>
<td align="center">玩家主动退出房间时</td>
<td align="center">操作Bmob数据库</td>
</tr>
<tr>
<td align="center">onReady</td>
<td align="center">玩家在房间内准备时</td>
<td align="center">-</td>
</tr>
<tr>
<td align="center">onUnready</td>
<td align="center">玩家取消准备</td>
<td align="center">-</td>
</tr>
<tr>
<td align="center">onGameStart</td>
<td align="center">本轮游戏开始</td>
<td align="center">初始化玩家属性</td>
</tr>
<tr>
<td align="center">onTick</td>
<td align="center">游戏开始后以一定频率被调用</td>
<td align="center">更新安全区位置和半径、发送通知，需要加上 <code>@BmobGameSDKHook</code> 注解</td>
</tr>
<tr>
<td align="center">onGameOver</td>
<td align="center">游戏结束</td>
<td align="center">保存游戏记录到Bmob数据库</td>
</tr>
<tr>
<td align="center">onOffline</td>
<td align="center">玩家掉线</td>
<td align="center">可通知其它玩家</td>
</tr>
<tr>
<td align="center">onReconn</td>
<td align="center">玩家重连</td>
<td align="center">更新一些自定义数据到本玩家，并通知其它玩家</td>
</tr>
<tr>
<td align="center">onKicked</td>
<td align="center">玩家被踢出房间</td>
<td align="center">-</td>
</tr>
</tbody>
</table>
<p><strong>Player.java 允许自定义Hook方法</strong></p>
<p>以下可以作为Hook方法
1. 获取属性
2. 修改属性
3. 监听属性变化
4. 处理客户端事件</p>
<p><strong>这些方法都需要加上 <code>@BmobGameSDKHook</code> 注解</strong></p>
<h3 id="_3">获取属性<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h3>
<ul>
<li>要求该属性的Editable为true，也就是允许客户端修改的属性</li>
<li>需用public、native修饰该方法</li>
<li>返回类型即为属性类型</li>
<li>参数必须为空</li>
<li>必须为public方法</li>
<li>方法名为"get" + 属性名</li>
</ul>
<p>例如，需要获取玩家当前的position属性，添加方法：</p>
<pre><code>@BmobGameSDKHook
public native float[] getPosition();
</code></pre>
<h3 id="_4">修改属性<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h3>
<ul>
<li>要求该属性的Editable为false，也就是不允许客户端修改的属性</li>
<li>需用public、native修饰该方法</li>
<li>返回类型为void</li>
<li>参数有一个，类型为属性类型</li>
<li>调用syncToClient后才同步到客户端</li>
<li>方法名为"set" + 属性名</li>
</ul>
<p>例如，如果云函数需要修改玩家的hp属性，需要在 <code>Player.java</code> 添加方法：</p>
<pre><code>@BmobGameSDKHook
public native void setHp(int hp);
</code></pre>
<h3 id="_5">监听属性变化<a class="headerlink" href="#_5" title="Permanent link">&para;</a></h3>
<ul>
<li>要求该属性的Editable为true，也就是允许客户端修改的属性</li>
<li>需用public、strictfp修饰该方法</li>
<li>返回类型为void</li>
<li>参数必须为空</li>
<li>需要获取属性值的话，在方法内调用native getXXX方法</li>
<li>方法名为"onUpdate_" + 属性名</li>
</ul>
<p>例如需要监听玩家的position属性变动，添加方法：</p>
<pre><code>@BmobGameSDKHook
public strictfp void onUpdate_Position() {
    // TODO 与当前安全区进行计算，是否扣除玩家血量
}
</code></pre>
<h3 id="_6">处理客户端行为<a class="headerlink" href="#_6" title="Permanent link">&para;</a></h3>
<ul>
<li>需用public、strictfp修饰该方法</li>
<li>返回类型为void</li>
<li>参数必须为byte[]</li>
<li>客户端在调用CloudAction方法时，传入的第一个参数作为"行为名"</li>
<li>方法名为"onAction_" + 行为名</li>
</ul>
<p>例如客户端上报击中其它玩家，<code>Action</code> 为 <code>Damage</code>，云端添加方法</p>
<pre><code>@BmobGameSDKHook
public strictfp void onAction_Damage(byte[] damage) {
    // 使用setHp修改被击中玩家的血量，如果&lt;=0，则判定死亡，通知所有用户
}
</code></pre>
<hr />
<h2 id="bmob">Bmob<a class="headerlink" href="#bmob" title="Permanent link">&para;</a></h2>
<h3 id="_7">初始化<a class="headerlink" href="#_7" title="Permanent link">&para;</a></h3>
<p>建议在Room.java文件内，添加一个static代码块进行初始化</p>
<pre><code>package cn.bmob.gamesdk.server.custom;

import cn.bmob.gamesdk.server.api.Bmob;

public class Room extends RoomBase {

    static {
        Bmob.init(&quot;Bmob AppKey&quot;, &quot;Bmob Restful Key&quot;);
    }

    ...
}

</code></pre>

<h3 id="_8">操作数据库<a class="headerlink" href="#_8" title="Permanent link">&para;</a></h3>
<ul>
<li>
<p>请参考 <a href="http://doc.bmob.cn/cloud_function/java/">Bmob Java云函数</a> 的 <code>modules.oData</code> </p>
</li>
<li>
<p>示例：在房间创建时，自动往数据库添加房间数据</p>
</li>
</ul>
<pre><code>...
public class Room extends RoomBase {
    public volatile String mRoomObjectId;

    @Override
    public void onCreate() {
        mRoomObjectId = Bmob.getInstance().insert(&quot;Room&quot;, JSON.toJson(//
                &quot;rid&quot;, roomId, // 房间Id
                &quot;joinKey&quot;, joinKey, // 加入房间需要的Key
                &quot;master&quot;, new BmobObject.BmobPointer(&quot;_User&quot;, masterId), // 房主Pointer类型
                &quot;masterKey&quot;, masterKey, // 房主密钥
                &quot;isPublic&quot;, false // 房间是否开放匹配，默认不开放
                )).jsonData.getString(&quot;objectId&quot;);
    }
...
</code></pre>
                </div>
            </div>
        </div>

        
            <script>var base_url = '../../..';</script>
            <script src="../../../js/jquery-1.10.2.min.js"></script>
            <script src="../../../js/bootstrap-3.0.3.min.js"></script>
            <script src="../../../js/highlight.pack.js"></script>
            <script src="../../../js/main.js"></script>
            <script src="../../../js/base.js"></script>

        <div id="go-top"><i class="fa fa-chevron-up"></i></div>
        <a href="https://docs.bmob.cn/data/Android/a_faststart/doc/index.html" id="back" target="_blank">返回 <br> 旧版</a>
    </body>

</html>